package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MarketMerchantProductSales;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.domain.enumclass.MerchantCooperationType;
import com.rockcent.mall.lib.dto.MarketMerchantProductSalesDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by oyhk on 14-6-22.
 */
@Repository
public class MarketMerchantProductSalesRepositoryImpl implements MarketMerchantProductSalesRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MarketMerchantProductSalesRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<MarketMerchantProductSalesDto> findDtoByDistributorMerchantIdAndSupplierMerchantIdAndIsDelete(MerchantCooperationType merchantCooperationType, Long distributorMerchantId, Long supplierMerchantId, String isDelete) {
        SQLListDoing<MarketMerchantProductSalesDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from market_merchant_product_sales mmps left join product p on mmps.product_id = p.market_id left join product_quantity pq on p.product_quantity_id = pq.id where p.merchant_id = :merchantId and mmps.distributor_status = 'PASS' and mmps.supplier_status = 'PASS' and mmps.is_delete = :isDelete and mmps.distributor_merchant_id = :distributorMerchantId and mmps.supplier_merchant_id = :supplierMerchantId  AND p.status  in (\"SOLDOUT\", \"EXPIRED\", \"SELLING\") order by mmps.date_created desc ");
            fields.append("mmps.*, p.market_id productId,p.name productName,p.type productType,p.base_price productBasePrice,p.sell_price productSellPrice,pq.quantity productQuantity,p.date_disabled productDateDisabled,p.status productStatus");
            params.put("merchantId",distributorMerchantId);
            params.put("distributorMerchantId", distributorMerchantId);
            params.put("supplierMerchantId", supplierMerchantId);
            params.put("isDelete",isDelete);
        };
        return doing.go(MarketMerchantProductSalesDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public MarketMerchantProductSales findByProductIdAndSupplierMerchantIdAndDistributorMerchantIdAndIsDeleteInPass(Long productId, Long merchantId, Long distributorMerchantId, Whether no) {

        SQLObjectDoing<MarketMerchantProductSales> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from market_merchant_product_sales p");
            sql.append(" where product_id= :productId");
            sql.append(" and is_delete= 'NO'");
            sql.append(" and supplier_merchant_id= :merchantId");
            sql.append(" and distributor_merchant_id= :distributorMerchantId");
            sql.append(" and supplier_status = 'PASS'");
            params.put("productId", productId);
            params.put("merchantId", merchantId);
            params.put("distributorMerchantId", distributorMerchantId);
            fields.append("p.*");
        };

        return result.go(MarketMerchantProductSales.class, jdbcTemplate, log, objectMapper);

    }
}
